#include "../login/mylogin.h"
#include "../login/myloginicon.h"
#include "../login/myloginqrcode.h"
#include "ui_mylogin.h"
#include <QDebug>
#include <QPainter>
#include <QMouseEvent>
#include "../common/myalert.h"
#include "../common/mymask.h"
#include "../../models/usermodel.h"

MyLogin::MyLogin(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::MyLogin)
{
    ui->setupUi(this);

    //登录页样式
    this->styleSelf();

    //关闭按钮
    this->styleCloseButton();

    //上线/隐身按钮状态
    this->styleOnlineButton();

    //账号、密码输入框
    this->styleAccountAndPasword();

    //登陆按钮
    this->styleLoginButton();

    //under按钮
    this->styleUnderButton();
}

MyLogin::~MyLogin()
{
    delete ui;
}

/************************************ style sheet begin********************************/
void MyLogin::styleSelf(){
    this->setStyleSheet("background-color:#f7f7f7;border-width:0;border-style:outset;border:0px;border-radius:10px");
    //this->setWindowFlags(Qt::SplashScreen);//隐藏任务栏，无边框模式
    this->setWindowFlags(Qt::FramelessWindowHint);

    //this->setAttribute((Qt::WA_DeleteOnClose));
}

void MyLogin::styleAccountAndPasword(){
    ui->account->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#c4c5c7");
    ui->account->setAttribute(Qt::WA_MacShowFocusRect, 0);//摆脱焦点蓝色边框
    ui->account->setPlaceholderText("Input Your XiaoH ID");

    ui->password->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#c4c5c7");
    ui->password->setEchoMode(QLineEdit::Password);//密码模式
    ui->password->setAttribute(Qt::WA_MacShowFocusRect, 0);
    ui->password->setPlaceholderText("Password");
}

void MyLogin::styleLoginButton(){
    ui->loginButton->setStyleSheet(LOGIN_BUTTON_INIT);
    ui->loginButton->setEnabled(false);
}

void MyLogin::styleCloseButton(){
    ui->closeButton->setStyleSheet("background-color:red;color:white;border-radius:6px;padding-bottom:3px;");
}

void MyLogin::styleOnlineButton(){
    ui->statusButton->setStyleSheet("QPushButton{border-image:url(:/icon/source/login/point-online-down.png)}");
}

void MyLogin::styleUnderButton(){
    ui->underButton->setStyleSheet(UNDER_BUTTON_NORMAL);
}
/************************************ style sheet end ********************************/

void MyLogin::on_loginButton_clicked()
{
    UserModel* user = UserModel::NewInstance();
    if(!user->login(account, password, 1)){
        MyAlert alertObj;
        alertObj.setTitle("Login Failed");
        alertObj.setMsg("发送失败，请检查网络或服务器未连接");
        alertObj.setButtonContent("OK");
        alertObj.exec();
    }
}

void MyLogin::apiLoginCallback(int code, QString msg){
    if(code == ResponseStruct::XIAOH_ROUTE_CODE_SUCCESS){
        //登陆成功
        //1、关闭当前窗口
        this->accept();
    }else{
        MyAlert alertObj;
        alertObj.setTitle("Login Failed");
        alertObj.setMsg(msg);
        alertObj.setButtonContent("OK");
        alertObj.exec();
    }
}


void MyLogin::on_closeButton_clicked()
{
    this->reject();
}

void MyLogin::mousePressEvent(QMouseEvent *event){

    if(event->button() == Qt::LeftButton){
        m_moving = true;
        //鼠标按下时，鼠标相对屏幕的位置减去窗口相对屏幕的位置
        //event->globalPos()和pos()返回(x, y)，x相对于屏幕左下角距离。y相对于屏幕左上角距离
        m_lastPos = event->globalPos() - pos();
    }
    return QDialog::mousePressEvent(event);
}

void MyLogin::mouseMoveEvent(QMouseEvent *event){
    if (m_moving  && (event->globalPos()-m_lastPos).manhattanLength() > QApplication::startDragDistance())
    {
        move(event->globalPos()-m_lastPos);
        m_lastPos = event->globalPos() - pos();
    }
    return QDialog::mouseMoveEvent(event);
}

void MyLogin::mouseReleaseEvent(QMouseEvent *event){
    m_moving = false;
    return QDialog::mouseReleaseEvent(event);
}

void MyLogin::on_statusButton_clicked()
{
    if(status_online == true){
        status_online = false;
        ui->statusButton->setStyleSheet("QPushButton{border-image:url(:/icon/source/login/point-invisible-down.png)}");
    }else{
        status_online = true;
        ui->statusButton->setStyleSheet("QPushButton{border-image:url(:/icon/source/login/point-online-down.png)}");
    }

}

void MyLogin::on_account_textChanged(const QString &arg1)
{
    account = arg1;
    if(account == ""){
        ui->account->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#c4c5c7");
    }else{
        ui->account->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#black");
    }

    if(account != "" && password != ""){
        ui->loginButton->setEnabled(true);
        ui->loginButton->setStyleSheet(LOGIN_BUTTON_INPUT);
    }else{
        ui->loginButton->setEnabled(false);
        ui->loginButton->setStyleSheet(LOGIN_BUTTON_INIT);
    }
}



void MyLogin::on_password_textChanged(const QString &arg1)
{
    password = arg1;

    if(password == ""){
        ui->password->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#c4c5c7");
    }else{
        ui->password->setStyleSheet("background:transparent;border-width:0;border-style:outset;color:#black");
    }

    if(account != "" && password != ""){
        ui->loginButton->setEnabled(true);
        ui->loginButton->setStyleSheet(LOGIN_BUTTON_INPUT);
    }else{
        ui->loginButton->setEnabled(false);
        ui->loginButton->setStyleSheet(LOGIN_BUTTON_INIT);
    }
}


void MyLogin::paintEvent(QPaintEvent *){
    //输入框下面画直线
    QPainter painter(this);
    painter.setPen(QPen(Qt::gray, 0.5));
    painter.drawLine(32, 224, 212, 224);
    painter.drawLine(32, 263, 212, 263);
}
